package net.lgpage.tool;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class FragmentDispatcher {

	public static void changeFM(FragmentActivity ac,int id,Fragment fg){
		FragmentManager mng = ac.getSupportFragmentManager();
		FragmentTransaction trans  = mng.beginTransaction();
		trans.replace(id, fg, fg.getClass().getSimpleName());
		trans.commit();
	}
	public static void addFM(FragmentManager fm,int id,Fragment fg){
		FragmentTransaction ts = fm.beginTransaction();
		ts.addToBackStack(null);
		ts.add(id, fg, fg.getClass().getName());
		ts.commit();
	}
	
	public static void removeFragments(FragmentManager fm) {
		if (fm == null) {
			return;
		}
		FragmentTransaction trans = fm.beginTransaction();
		List<Fragment> list = fm.getFragments();
		if (list != null)
			for (int i = 0; i < list.size(); i++) {
				trans.remove(list.get(i));
			}
		trans.commit();
	}

	public static void removeFragmentsAndAdd(FragmentManager fm,
			int id, Fragment fg) {
		if (fm == null) {
			return;
		}
		FragmentTransaction trans = fm.beginTransaction();
		List<Fragment> list = fm.getFragments();
		if (list != null)
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i) != null)
					trans.remove(list.get(i));
			}
		trans.add(id, fg,fg.getClass().getName());
		trans.commit();
	}
	
}
